package com.example.demo.mapper;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.text.StrBuilder;
import cn.hutool.core.util.StrUtil;
import com.example.demo.config.jdbc.MyEasyJdbcTemplate;
import com.example.demo.pojo.User;
import com.xphsc.easyjdbc.page.PageInfo;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.thymeleaf.expression.Lists;

import java.util.ArrayList;
import java.util.List;

@Repository
public class UserMapper {

    @Autowired
    MyEasyJdbcTemplate jdbcTemplate;

    public User findByUserName(String username) {
        String sql = "select * from t_user where username =?";
        User o = jdbcTemplate.get(sql, User.class, username);
        return o;
    }

    public void save(User user) {

        jdbcTemplate.insert(user);
    }

    public List<User> findList(Integer offset, Integer pageSize, String type) {
        ArrayList<Object> params = CollUtil.newArrayList();
        StrBuilder sql = StrBuilder.create();
        sql.append(" SELECT ");
        sql.append(" 	*  ");
        sql.append(" FROM ");
        sql.append(" 	t_user  ");
        sql.append(" WHERE ");
        sql.append(" 	1 = 1  ");
        if (StrUtil.isNotBlank(type)) {
            sql.append(" 	AND type = ?  ");
            params.add(type);
        }
        sql.append(" 	LIMIT ?,? ");
        params.add(offset);
        params.add(pageSize);
        List<User> users = jdbcTemplate.find(sql.toString(), User.class, params);
        return users;
    }

    public long findCount(Integer offset, Integer pageSize, String type) {
        ArrayList<Object> params = CollUtil.newArrayList();
        StrBuilder sql = StrBuilder.create();
        sql.append(" SELECT ");
        sql.append(" 	count(*)  ");
        sql.append(" FROM ");
        sql.append(" 	t_user  ");
        sql.append(" WHERE ");
        sql.append(" 	1 = 1  ");
        if (StrUtil.isNotBlank(type)) {
            sql.append(" 	AND type = ?  ");
            params.add(type);
        }
        long count = jdbcTemplate.count(sql.toString(), User.class, params);

        return count;
    }

    public PageInfo<User> findPage(Integer offset, Integer pageSize, String type) {
        StrBuilder sql = StrBuilder.create();
        ArrayList<Object> params = new ArrayList<>();
        sql.append(" SELECT ");
        sql.append(" 	*  ");
        sql.append(" FROM ");
        sql.append(" 	t_user  ");
        sql.append(" WHERE ");
        sql.append(" 	1 = 1  ");
        if (StrUtil.isNotEmpty(type)) {
            sql.append("  AND type = ?  ");
            params.add(type);
        }
        sql.append("  order by id desc ");
        PageInfo<User> byPage = jdbcTemplate.findByPage(sql.toString(), User.class, offset, pageSize, params.toArray());
        return byPage;
    }

    public void del(String userid) {
        String sql = "DELETE FROM t_user WHERE id = ?";
        jdbcTemplate.delete(sql, userid);

    }

    public User findById(long userid) {
        String sql = "select * from t_user where id =?";
        User o = jdbcTemplate.get(sql, User.class, userid);
        return o;
    }

    public void update(User user) {
        jdbcTemplate.update(user);
    }
}
